﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using Jumpton.Data;
using System.Data;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.MasterData;

namespace Jumpton.SFA.Server.BusinessRule.Traffic
{
    public class DeliverGpsManager : BusinessLogic
    {
        public DELIVERY_GPS Load(decimal ID)
        {
            return (DELIVERY_GPS)ObjectRow.Load(typeof(DELIVERY_GPS), string.Format("this.ID={0}", ID));
        }

        public decimal Save(DELIVERY_GPS DeliveryGps)
        {
            if (DeliveryGps != null)
            {
                try
                {
                    DeliveryGps.Save();
                }
                catch (Exception err)
                {
                    message = err.Message; throw err;
                }

                return DeliveryGps.ID;
            }

            return 0;
        }

        public byte[] GetCoordinate(string OrderNo)
        {
            ORDER_HEADERS order = new Jumpton.SFA.Server.BusinessRule.OrderHeaderManager().Load(OrderNo);
            if (order == null)
            {
                return null;
            }

            DELIVERY_ROUTE route = (DELIVERY_ROUTE)ObjectRow.Load(typeof(DELIVERY_ROUTE), string.Format("this.ORDER_ID='{0}'", OrderNo));
            if (route == null)
            {
                return Encoding.UTF8.GetBytes(order.STATUS.ToString());
            }

            DELIVERY_GPS gps = (DELIVERY_GPS)ObjectRow.Load(typeof(DELIVERY_GPS), string.Format("this.TRUCKS_CODE='{0}' and CREATE_TIME = (select max(CREATE_TIME) from DELIVERY_GPS)", route.TRUCKS_CODE));
            

            return Encoding.UTF8.GetBytes(order.STATUS.ToString() + "," + gps.LONGITUDE.ToString() + "," + gps.LATITUDE.ToString());
        }
    }
}
